package BLC

import (
	"fmt"
	"github.com/boltdb/bolt"
	"os"
)

// 转账
func (cli *CLI) send(from string,to string,amount int)  {


	if dbExists() == false {
		fmt.Println("数据不存在.......")
		os.Exit(1)
	}

	blockchain := BlockChainObject()
	defer blockchain.DB.Close()

	transaction:=NewSimpleTransaction(from,to,amount,blockchain)

	var Height int64
	var Hash []byte
	err:=blockchain.DB.View(func (tx *bolt.Tx)error{
		b:=tx.Bucket([]byte(blockBucket))

		if b!=nil{
			blockHash:=b.Get([]byte("l"))
			blockSeriler:=b.Get(blockHash)
			block:=UnSerilizeBlock(blockSeriler)
			Height=block.Height
			Hash=block.Hash
		}

		return nil
	})

	if err!=nil{
		panic(err)
	}

	blockchain.MineNewBlock([]*Transaction{transaction},Height+1,Hash)
}
